﻿//----------------------------------------------------------------
// <copyright file="PrivilegeBLL.cs" >
//    Copyright (c) Wenzy , All rights reserved.
//    author:温正宇 E-Mail:wenzy@tom.com　MyBlog:wenzy.cnblogs.com
//    All rights reserved.
// </copyright>
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using MemberShipDAL.DataObject;

namespace WMFConfig.Bussiness
{
    class PrivilegeBLL
    {
        /// <summary>
        /// 权限数据列表绑定
        /// </summary>
        /// <param name="GropuCNName">组的中文名</param>
        /// <param name="OTable">操作数据表</param>
        /// <param name="RTable">资源数据表</param>
        /// <param name="dgView">要绑定的DataGrid</param>
        public bool PrivilegeBind(string GropuId, DataResources.wmf_OperationDataTable OTable, DataResources.wmf_ResourcesDataTable RTable, DataResources.wmf_PrivilegeDataTable PTable, DataGridView dgView)
        {
            //创建一张数据表
            DataTable dt = new DataTable("ResourceaAndPrivilege");

            //生成这张表的列

            foreach (DataResources.wmf_OperationRow row in OTable)
            {
                DataColumn dc = new DataColumn(row.OperationName, typeof(bool));
                dt.Columns.Add(dc);
            }

            DataView dv = RTable.DefaultView;
            //如果有传入组Id,那么就只绑定关于这个组的内容。
            if (GropuId != string.Empty)
            {
                dv.RowFilter = "[ResourceGroupId] ='" + GropuId + "'";
            }
            //添加数据
            foreach (DataRowView row in dv)
            {
                DataRow dr = dt.NewRow();
                foreach (DataResources.wmf_OperationRow optrow in OTable)
                {
                    dr[optrow.OperationName] = GetPrivilege(optrow.OperationId, row["ResourcesId"].ToString(), PTable);
                }
                dt.Rows.Add(dr);
            }

            dgView.DataSource = dt;

            //设置列长度以及标题
            foreach (DataGridViewColumn dgColumn in dgView.Columns)
            {
                DataView dvOperation = OTable.DefaultView;
                dvOperation.RowFilter = "[OperationName] = '" + dgColumn.HeaderText + "'";
                if (dvOperation.Count == 1)
                {
                    dgColumn.HeaderText = dvOperation[0]["OperationCNName"].ToString();
                    //用Tag来存放OperationId;
                    dgColumn.HeaderCell.Tag = dvOperation[0]["OperationId"].ToString() + "|" + dvOperation[0]["OperationName"].ToString();
                    dgColumn.HeaderCell.ToolTipText = dvOperation[0]["Description"].ToString();
                }
                dgColumn.Width = 50;
            }

            //把权限名称写入表头
            foreach (DataGridViewRow row in dgView.Rows)
            {
                row.HeaderCell.Value = dv[row.Index]["ResourcesCNName"].ToString();
                //用ToolTipText来存放ResourcesId
                row.HeaderCell.Tag = dv[row.Index]["ResourcesId"].ToString() + "|" + dv[row.Index]["ResourcesName"].ToString();
                //ToolTipText用来显示描述
                row.HeaderCell.ToolTipText = dv[row.Index]["Description"].ToString();
            }
            return true;

        }

        /// <summary>
        /// 获取在wmf_Privilege表中是否存对应的记录
        /// </summary>
        /// <param name="optId"></param>
        /// <param name="resId"></param>
        /// <returns></returns>
        private bool GetPrivilege(int optId, string resId, DataTable dsPrivilege)
        {
            DataView dv = dsPrivilege.DefaultView;
            dv.RowFilter = "[OperationId] = " + optId + " and [ResourcesId] ='" + resId + "'";
            if (dv.Count == 1)
            {
                return true;
            }
            return false;
        }



    }
}
